package com.shujia.day20.lambda4;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
    使用匿名内部类改进
 */
public class StaffDemo {
    private static final List<Staff> list = Arrays.asList(
            new Staff("sj1001", "李刚", 18, 35000),
            new Staff("sj1002", "钱志强", 13, 20000),
            new Staff("sj1003", "江川", 24, 50000),
            new Staff("sj1004", "祝帅", 16, 21000),
            new Staff("sj1005", "吴问强", 8, 8000)
    );

    public static void main(String[] args) {
        // 需求1：过滤出薪资大于30000的员工
//        List<Staff> staffs1 = filterWithSalary(list);

//        List<Staff> staffs1 = filterWithCondition(list, new FilterStaff() {
//            @Override
//            public boolean filterWithCondition(Staff staff) {
//                return staff.getSalary() > 30000;
//            }
//        });

        List<Staff> staffs1 = filterWithCondition(list, (Staff s)->{
            return s.getSalary()>30000;
        });
        for (Staff staff : staffs1) {
            System.out.println(staff);
        }

        System.out.println("--------------------------------");
        // 需求2：过滤出年龄小于18的员工
//        List<Staff> staffs2= filterWithAge(list);
//        List<Staff> staffs2 = filterWithCondition(list, new FilterStaff() {
//            @Override
//            public boolean filterWithCondition(Staff staff) {
//                return staff.getAge()<18;
//            }
//        });

        //使用lambda表达式改写
        List<Staff> staffs2 = filterWithCondition(list, (Staff s) -> {
            return s.getAge()<18;
        });

//        List<Staff> staffs2 = filterWithCondition(list, s->s.getAge()<18);

        for (Staff staff : staffs2) {
            System.out.println(staff);
        }


    }

    public static List<Staff> filterWithCondition(List<Staff> list, FilterStaff filterStaff) {
        ArrayList<Staff> list2 = new ArrayList<>();
        for (Staff staff : list) {
            if (filterStaff.filterWithCondition(staff)) {
                list2.add(staff);
            }
        }
        return list2;
    }

//    public static List<Staff> filterWithAge(List<Staff> list){
//        ArrayList<Staff> list2 = new ArrayList<>();
//        for (Staff staff : list) {
//            if(staff.getAge()<18){
//                list2.add(staff);
//            }
//        }
//        return list2;
//    }
//
//    public static List<Staff> filterWithSalary(List<Staff> list){
//        ArrayList<Staff> list2 = new ArrayList<>();
//        for (Staff staff : list) {
//            if(staff.getSalary()>30000){
//                list2.add(staff);
//            }
//        }
//        return list2;
//    }
}
